
# plot graph by R 
$code = 1628;
$start = "1990-1-1";
$end = "2010-10-1";
$r_sql = "select exchange_date as date, ( adjusted_end_price * volume ) as value from stock_exchange_ja_table ";
$r_sql .= "where code = '$code' and exchange_date between '$start' and '$end' order by exchange_date;";
$r_sql2 = "select exchange_date as date, adjusted_end_price as value from stock_exchange_ja_table ";
$r_sql2 .= "where code = '$code' and exchange_date between '$start' and '$end' order by exchange_date";
$r_script = "library(RpgSQL)\n";
$r_script .= "db <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )\n";

$r_script .= "sql = \"$r_sql;\"\n";
$r_script .= "res <- dbGetQuery( db, sql )\n";

$r_script .= "sql2 = \"$r_sql2;\"\n";
$r_script .= "res2 <- dbGetQuery( db, sql2 )\n";

$r_script .= "dbDisconnect( db )\n";

$r_script .= "png('valume.png', width = 1440, height = 480, pointsize = 12, bg = 'white' )\n";
$r_script .= "plot( res\$date, res\$value, col='red', type='l' );\n";
$r_script .= "dev.off()\n";
$r_script .= "png('end_price.png', width = 1440, height = 480, pointsize = 12, bg = 'white' )\n";
$r_script .= "plot( res2\$date, res2\$value, col='blue', type='l' );\n";
$r_script .= "dev.off()\n";
open FILE, ">script.r";
print FILE $r_script;
close FILE;
system "R --no-save <script.r";
